<?php

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
#	FILE:			site_specific/sponsor_browse.php
#	FUNCTION:		Handles the sponsor browsing pages - Accomm, Services, Tours, Transport, Work
#	AUTHOR:			Cameron Morrow
#	CREATED:		28/09/2005
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# GET PASSED VARS
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# Check if browsing a region
$region_type = (@$_GET["region"]) ? $_GET["region"] : "r-1";

# Strip R
$region_type = (integer) substr($region_type, 1);

# Sponsor
$sponsor = (@$_GET["sponsor"]) ? removeInvalidChars($_GET["sponsor"], array_merge($VALID_CHAR_LIST["ids"], $VALID_CHARS_LETTERS_UPPER)) : "";

# Search
$SEARCH_TERMS = (@$_GET["search"] ? $_GET["search"] : "");

# Identify the Category
if (@$EXTERNAL_CONSTANTS["categoryforpage"][$PAGE_ID]) {
	$category = $EXTERNAL_CONSTANTS["categoryforpage"][$PAGE_ID];
	$category_name = $EXTERNAL_CONSTANTS["sponsor_categories"][$category];
} else {
	$category = false;
	$category_name = "Unknown Type: ";
}


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# GET EXISTING CONTENT
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

$page_content = parseFlashLinks(preg_replace(array("/<br>/i", "/\n/i"), array("<br />", ""), parseFlashXML($PAGE -> getData("p_content"))));

# Extract sidebar
$second_column_content = extractSidebar($page_content);

# Remove sidebar from content
$page_content = removeSidebar($page_content);

# Add (unless viewing a profile)
if ($sponsor != "" || $region_type != -1) {
} else {
	addContent($page_content);
}


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# IF BROWSING A REGION OR VIEWING A PROFILE
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

if ($region_type != -1) {


	# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	# VIEWING A SPONSOR
	# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	if ($sponsor != "") {

		# Get details
		$sql = "SELECT * FROM " . $PROJECT_DB_TABLES["users"] . " WHERE u_id = '" . $sponsor . "'";
		$details = getRecord($sql);

		if (count($details) == 1 && $details !== false) {

			# Get first result
			$sponsor_data = $details[0];

			# Set page name
			$PAGE_NAME = $sponsor_data["u_login_name"];

			# Image
			$image = explode("|", $sponsor_data["u_image"]);
			if ($image[0] != "") {
				$image = "<img src=\"../assets/uploads/" . $image[0] . "\" alt=\"Image\" width=\"380\" style=\"margin: 0; border: 1px solid #666; padding: 1px;\" />";
			} else {
				$image = "";
			}

			$image2 = explode("|", $sponsor_data["u_image_b"]);
			if ($image2[0] != "") {
				$image2 = "<img src=\"../assets/uploads/" . $image2[0] . "\" alt=\"Image 2\" width=\"380\" style=\"margin: 10px 0 0 0; border: 1px solid #666; padding: 1px;\" />";
			} else {
				$image2 = "";
			}

			$image3 = explode("|", $sponsor_data["u_image_c"]);
			if ($image3[0] != "") {
				$image3 = "<img src=\"../assets/uploads/" . $image3[0] . "\" alt=\"Image 3\" width=\"380\" style=\"margin: 10px 0 0 0; border: 1px solid #666; padding: 1px;\" />";
			} else {
				$image3 = "";
			}

			$image = $image . $image2 . $image3;

			# Contact Detail
			addContent("" .

				"$image" .

				# Desc
				"<h4>Description:</h4>" .
				($sponsor_data["u_desc"] == "" ? "<p>None provided.</p>" : "<p>" . $sponsor_data["u_desc"] . "</a></p>") .

				# URL
				"<h4>Website:</h4>" .
				($sponsor_data["u_url"] == "" ? "<p>None provided.</p>" : "<p><a target=\"_blank\" href=\"" . fixURL($sponsor_data["u_url"]) . "\">" . fixURL($sponsor_data["u_url"]) . "</a></p>") .

				# Contact Details
				"<h4 style=\"clear: both;\">Contact:</h4>" .
				"<p>Email: <a href=\"mailto:" . $sponsor_data["u_email"] . "\">Click to send an email</a></p>" .
				"<p>Phone: " . $sponsor_data["u_phone"] . "</p>" .
				#"<p>Fax: " . $sponsor_data["u_fax"] . "</p>" .
				#"<p>Mobile: " . $sponsor_data["u_mobile"] . "</p>" .

				# Address
				"<h4>Address:</h4><p>" . str_replace("\n", "<br />", $sponsor_data["u_address"]) . "</p>" .

				# Regions
				"<h4>Regions:</h4><ul class=\"regionlist\">");

			# Regions
			$regions = explode(",", $sponsor_data["u_region"]);
			foreach ($regions as $region_id) {
				addContent("<li><a href=\"?region=r" . $region_id . "\">" . $EXTERNAL_CONSTANTS["regions"][$region_id] . "</a></li>");
			}

			# End
			addContent("</ul>");

			if ($SEARCH_TERMS != "") {
				addContent("<h4><a href=\"search.php?search=" . $SEARCH_TERMS . "\">&laquo; Return to search results</a></h4>");
			}

		} else {

			addContent("<p>Sorry, an error occured and the requested sponsor could not be displayed.</p>");
		}


	# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	# BROWSING A REGION
	# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	} else {

		# Link to region page
		addContent("<h4>More about <a href=\"../site/" . $EXTERNAL_CONSTANTS["regionpage"][$region_type] . ".php\">" . $EXTERNAL_CONSTANTS["regions"][$region_type] . "</a></h4>" .
		#	"<p>See the <a href=\"../site/" . $EXTERNAL_CONSTANTS["regionpage"][$region_type] . ".php\">" . $EXTERNAL_CONSTANTS["regions"][$region_type] . "</a> page, or return to the main <a href=\"" . $PAGE_ID . ".php\">" . $EXTERNAL_CONSTANTS["sponsor_categories"][$category] . "</a> page.</p>" .
		"");

		# Header
		#addContent("<h4>Items:</h4>");

		# Get items for this region
		$sql = "SELECT u_id, u_login_name, u_desc, u_image FROM " . $PROJECT_DB_TABLES["users"] . " WHERE (u_category = '" . $category . "' OR u_category LIKE '%," . $category . "' OR u_category LIKE '" . $category . ",%' OR u_category LIKE '%," . $category . ",%') AND (u_region = '" . $region_type . "' OR u_region LIKE '%," . $region_type . "' OR u_region LIKE '" . $region_type . ",%' OR u_region LIKE '%," . $region_type . ",%') AND u_group = " . getBusinessGroupID() . " ORDER BY u_login_name ASC";


		# sb_arrow_more.gif, sb_arrow_less.gif

		# Get results
		$results = getRecord($sql);

		# If results
		if (count($results) > 0) {

			addContent("<ul class=\"sponsorlist\">");

			foreach ($results as $count => $record) {

				# Image
				if ($record["u_image"] != "") {
					$image = explode("|", $record["u_image"]);
					$image = "<img src=\"../assets/uploads/" . $image[0] . "\" alt=\"Image\" style=\"float: right; margin: 0; padding: 4px 10px;\" width=\"100\" />";
				} else {
					$image = "";
				}

				addContent("<li id=\"liresult" . $count . "\">" .
					"<h4><span class=\"moreinfo\"><a name=\"result" . $count . "\" href=\"#result" . $count . "\" onclick=\"toggleResult(" . $count . ");\"><img id=\"result" . $count . "toggle\" src=\"../site_specific/assets/images/sb_arrow_more.gif\" alt=\"Toggle\" /></a></span>" .
					"<a href=\"?region=r" . $region_type . "&sponsor=" . $record["u_id"] . "\">" . $record["u_login_name"] . "</a></h4>" .
					"<div class=\"hidden\" id=\"result" . $count . "content\">" .
					$image .
					"<p>" . $record["u_desc"] . "</p>" .
					"<p style=\"clear: both;\"><a href=\"?region=r" . $region_type . "&sponsor=" . $record["u_id"] . "\">More Information &raquo;</a></p>" .
					"</div>" .
					"</li>");
			}

			addContent("</ul>");

		} else {

			addContent("<p>Sorry, no items were found in this region.</p>");
		}
	}

} else {

	addContent("<h4>Browse by Region:</h4>");
	addContent("<p>Select a region from the right.</p>");
}

?>